package com.lindroy.androidplayer.ui.adapter

import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentStatePagerAdapter
import com.lindroy.androidplayer.base.BaseFragment
import com.lindroy.androidplayer.ui.home.fragment.ProjectListFragment
import com.lindroy.androidplayer.ui.home.fragment.WXArticleFragment
import com.lindroy.androidplayer.ui.knowledge.KnowledgeDetailFragment

/**
 * @author Lin
 * @date 2021/4/9
 * @function TabLayout的适配器
 */
sealed class BaseTabAdapter(
    type: Int,
    private val tabList: List<TabBean>,
    fm: FragmentManager
) :
    FragmentStatePagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    private val fragments = arrayListOf<BaseFragment>()

    companion object {
        const val TAB_WX = 0
        const val TAB_PROJECT = 1
        const val TAB_KNOWLEDGE = 2
    }

    init {
        fragments.clear()
        when (type) {
            TAB_WX -> tabList.forEach { fragments.add(WXArticleFragment.newInstance(it.id)) }
            TAB_PROJECT -> tabList.forEach { fragments.add(ProjectListFragment.newInstance(it.id)) }
            TAB_KNOWLEDGE -> tabList.forEach { fragments.add(KnowledgeDetailFragment.newInstance(it.id)) }
        }
    }

    override fun getCount(): Int = tabList.size

    override fun getItem(position: Int): Fragment = fragments[position]

    //显示标签上的文字
    override fun getPageTitle(position: Int): CharSequence = tabList[position].name

    data class TabBean(val id: Int, val name: String)
}

class WXTabAdapter(tabs: List<TabBean>, fm: FragmentManager) :
    BaseTabAdapter(TAB_WX, tabs, fm)

class ProjectTabAdapter(tabs: List<TabBean>, fm: FragmentManager) :
    BaseTabAdapter(TAB_PROJECT, tabs, fm)

class KnowledgeTabAdapter(tabs: List<TabBean>, fm: FragmentManager) :
    BaseTabAdapter(TAB_KNOWLEDGE, tabs, fm)